// 获取设备信息
import { request } from '../index'
import device from '@system.device'
import storage from '@system.storage'
// const { deviceId, deviceModel, platform, appVersion, osVersion } = systemInfo
const visitorLogin = () => {
    return new Promise((resolve,reject)=>{
        device.getId({
            type: ['device', 'mac', 'user', 'advertising'],
            success: async function (data) {
                const deviceInfo = await device.getInfo()
                const ddata = deviceInfo.data
                console.log('设备信息:',JSON.stringify(ddata))
                const {
                    manufacturer,
                    osType,
                    vendorOsVersion,
                    osVersionCode,
                    osVersionName
                } = ddata
                request({
                    url: '/auth/visitor_login',
                    data: {
                        deviceCode: data.device || data.user,
                        model: manufacturer,
                        os: osType,
                        appVer: osVersionName || vendorOsVersion,
                        osVer: osVersionCode
                    }
                }).then(res => {
                    storage.set({
                        key: 'token',
                        value: res.token
                    })
                    storage.set({
                        key: 'loginUser',
                        value: JSON.stringify(res.loginUser)
                    })
                    resolve()
                }).catch(error => {
                    console.log('失败visitor_login:',JSON.stringify(error))
                })
            },
            fail: function (data, code) {
                console.log('获取设备信息错误',code,data)
            }
        })
        
    })
    
}

export default visitorLogin
